package org.jetnuke.dao.generic;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.dao.PictureAlbumDAO;
import org.jetnuke.entities.PictureAlbum;

public class GenericPictureAlbumDAO extends AutoKeys implements PictureAlbumDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GenericPictureAlbumDAO.class);

	public PictureAlbum retrieveByPK(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("PictureAlbumModel.selectByID"));
		p.setInt(1, id);

		ResultSet rs = p.executeQuery();
		PictureAlbum s = new PictureAlbum();

		rs.next();
		fillPictureAlbumFromResultSet(s, rs);

		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - end"); //$NON-NLS-1$
		}
		return s;
	}

	protected void fillPictureAlbumFromResultSet(PictureAlbum s, ResultSet rs)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("fillPictureAlbumFromResultSet(PictureAlbum, ResultSet) - start"); //$NON-NLS-1$
		}

		s.setAID(rs.getInt("AID"));
		s.setCAN_DELETE(rs.getInt("CANDELETE") == 1);
		s.setCID(rs.getInt("CID"));
		s.setCOMMENTS(rs.getString("COMMENTS").equals("YES"));
		s.setCREATION_DATE(rs.getTimestamp("CTIME"));
		s.setDESCRIPTION(rs.getString("DESCRIPTION"));
		s.setKEYWORDS(rs.getString("KEYWORDS"));
		s.setLAST_ADDITION(rs.getTimestamp("LAST_ADDITION"));
		s.setPOS(rs.getInt("POS"));
		s.setTHUMBNAIL(rs.getInt("THUMB"));
		s.setTITLE(rs.getString("TITLE"));
		s.setUID(rs.getInt("UID"));
		s.setVISIBILITY(rs.getInt("VISIBILITY"));
		s.setVOTES(rs.getString("COMMENTS").equals("YES"));

		if (logger.isDebugEnabled()) {
			logger
					.debug("fillPictureAlbumFromResultSet(PictureAlbum, ResultSet) - end"); //$NON-NLS-1$
		}
	}

	public List selectByUser(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectByUser(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("PictureAlbumModel.selectByUser"));
		p.setInt(1, id);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectByUser(int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	protected List processSelectAll(ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - start"); //$NON-NLS-1$
		}

		List list = new ArrayList();
		while (rs.next()) {
			PictureAlbum u = new PictureAlbum();
			fillPictureAlbumFromResultSet(u, rs);
			list.add(u);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public int addNew(PictureAlbum pa) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("addNew(PictureAlbum) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = this
				.getStatementForAutoKeys("PictureAlbumModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals
				.getSql("PictureAlbumModel.lastGeneratedAlbumID"));
		initAlbum(pa, p);

		int id = this.executeAutoKeysQuery(p);
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("addNew(PictureAlbum) - end"); //$NON-NLS-1$
		}
		return id;

	}

	private void initAlbum(PictureAlbum pa, PreparedStatement p)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("initAlbum(PictureAlbum, PreparedStatement) - start"); //$NON-NLS-1$
		}

		GregorianCalendar gc = new GregorianCalendar();
		long initTime = gc.getTimeInMillis();
		Timestamp stamp = new Timestamp(initTime);
		p.setInt(1, pa.getCID());
		p.setTimestamp(2, stamp);
		p.setString(3, pa.getDESCRIPTION());
		p.setString(4, pa.getKEYWORDS());
		p.setTimestamp(5, stamp);
		p.setInt(6, pa.getPOS());
		p.setInt(7, pa.getTHUMBNAIL());
		p.setString(8, pa.getTITLE());
		p.setInt(9, pa.getUID());
		p.setInt(10, pa.getVISIBILITY());
		p.setInt(11, pa.isCAN_DELETE() == true ? 1 : 0);
		p.setString(12, pa.isCOMMENTS() == true ? "YES" : "NO");
		p.setString(13, pa.isVOTES() == true ? "YES" : "NO");

		if (logger.isDebugEnabled()) {
			logger.debug("initAlbum(PictureAlbum, PreparedStatement) - end"); //$NON-NLS-1$
		}
	}

	public void update(PictureAlbum pa) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("update(PictureAlbum) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("PictureAlbumModel.update"));
		p.setInt(1, pa.getCID());
		p.setTimestamp(2, new Timestamp(pa.getCREATION_DATE().getTime()));
		p.setString(3, pa.getDESCRIPTION());
		p.setString(4, pa.getKEYWORDS());
		p.setTimestamp(5, new Timestamp(pa.getLAST_ADDITION().getTime()));
		p.setInt(6, pa.getPOS());
		p.setInt(7, pa.getTHUMBNAIL());
		p.setString(8, pa.getTITLE());
		p.setInt(9, pa.getUID());
		p.setInt(10, pa.getVISIBILITY());
		p.setInt(11, pa.isCAN_DELETE() == true ? 1 : 0);
		p.setString(12, pa.isCOMMENTS() == true ? "YES" : "NO");
		p.setString(13, pa.isVOTES() == true ? "YES" : "NO");
		p.setInt(14, pa.getAID());
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("update(PictureAlbum) - end"); //$NON-NLS-1$
		}
	}

	public void deleteByAID(int aid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("deleteByAID(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("PictureAlbumModel.deleteByAID"));
		p.setInt(1, aid);
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("deleteByAID(int) - end"); //$NON-NLS-1$
		}
	}

}
